package cn.gov.msa.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;

import cn.gov.msa.dao.RepositoryMapper;
import cn.gov.msa.model.Repository;
import cn.gov.msa.model.RepositoryClassify;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.RepositoryService;

public class RepositoryServiceImpl implements RepositoryService{

	@Autowired
	private RepositoryMapper repositoryMapper;
	
	@Override
	public List<Repository> selectRepository(int pageNum, int pageSize, Repository repository) throws Exception {
		
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码  
		List<Repository> listRepositorys = repositoryMapper.selectData(repository);
		
		return listRepositorys;
	}

	@Override
	public boolean updateViewCount(Repository repository) throws Exception {
		boolean result = false;
		
		List<Repository> list = repositoryMapper.selectData(repository);
		String viewCount = list.get(0).getViewCount();
		int count = Integer.valueOf(viewCount)+1;
		repository.setViewCount(String.valueOf(count));
		int flag = repositoryMapper.updateViewCount(repository);
		
		if(flag>0) {
			result = true;
		}
		
		return result;
	}

	@Override
	public boolean saveData(Repository repository) throws Exception {
		boolean result = false;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        
        String date = sdf.format(new Date());
        repository.setReleaseTime(date);
        
        repository.setViewCount("0");//浏览次数默认是0
        
        repository.setStatus("1");//状态默认是已审核
		
        repository.setIssuer(repository.getSyscreatedbyname());//先写固定发布人
		
		int idict = repositoryMapper.insert(repository);
		if(idict > 0)
			result = true;
		
		return result;
	}

	@Override
	public List<RepositoryClassify> selectSmallClass(Repository repository) throws Exception {
 
		List<RepositoryClassify> listSmall = repositoryMapper.selectSmallClass(repository);
		
		return listSmall;
	}

	@Override
	public int deleteByPK(String repositoryPK) throws Exception {
		
		int result = repositoryMapper.deleteByPK(repositoryPK);
		
		return result;
	}

	@Override
	public boolean updateData(Repository repository) throws Exception {
		boolean result = false;
		String countFlag = repository.getCountFlag();
		if("1".equals(countFlag)) {
			repository.setViewCount("0");
		}

		int irepository = repositoryMapper.updateByPK(repository);

		if(irepository > 0)
			result = true;
		
		return result;
	}
	
	
	
	
	
	
	
	
	
	

	
	
}
